/*	Author: Dao Anh Vu
 *  This is DAO for Account and AccountType
 * */

package nautilus.placetaker.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import nautilus.placetaker.dto.Place;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AccountDAO {
	private SQLiteDatabase _database;
	private MoneyDataHelper _helper;
	//private SimpleDateFormat _format = new SimpleDateFormat("dd:MM:yyyy hh:mm:ss", SystemConfig.LOCALE);
	private SimpleDateFormat _format = new SimpleDateFormat("dd:MM:yyyy hh:mm:ss");
	
	public AccountDAO(Context context){
		_helper = new MoneyDataHelper(context);
		_database = _helper.getWritableDatabase();
		//Log.i(TAG, "Create Helper and get Database OK");
	}
	
	public AccountDAO(MoneyDataHelper helper){
		_helper = helper;
		_database = _helper.getWritableDatabase();
	}
	
	public void close(){
		_helper.close();
	}
	
	@Override
	public void finalize(){
		if(_helper != null)
			_helper.close();
	}
	
	public long addPlace(Place acc){
		ContentValues values = new ContentValues();
		//values.put("_id", acc.getId());
		values.put("_name", acc.getName());
		values.put("_address", acc.getAddress());
		values.put("_latitude", acc.getLatitude());
		values.put("_longitude", acc.getLongitude());
		values.put("_image1", acc.getImage(0));
		values.put("_image2", acc.getImage(1));
		values.put("_image3", acc.getImage(2));
		values.put("_image4", acc.getImage(3));
		values.put("_categoryId", acc.getCategoryId());
		values.put("_categoryName", acc.getCategoryName());
		values.put("_audio", acc.getAudioFile());
		values.put("_lastUpdate", acc.getLastUpdate());
		values.put("_description", acc.getDescription());
		values.put("_isChecked", acc.isCheck()?1:0);
		
		long id = _database.insert(MoneyDataHelper.PLACE_TABLE, null, values);
		acc.setId(id);
		return id;
		//Log.i(TAG, String.format("Insert account(%d, %s) into database successfully!", acc.getId(), acc.getName()));
	}
	
	public void updatePlace(Place acc){
		ContentValues values = new ContentValues();		
		values.put("_name", acc.getName());
		values.put("_address", acc.getAddress());
		values.put("_latitude", acc.getLatitude());
		values.put("_longitude", acc.getLongitude());
		values.put("_image1", acc.getImage(0));
		values.put("_image2", acc.getImage(1));
		values.put("_image3", acc.getImage(2));
		values.put("_image4", acc.getImage(3));
		values.put("_categoryId", acc.getCategoryId());
		values.put("_categoryName", acc.getCategoryName());
		values.put("_audio", acc.getAudioFile());
		values.put("_lastUpdate", acc.getLastUpdate());
		values.put("_description", acc.getDescription());
		values.put("_isChecked", acc.isCheck()?1:0);
		_database.update(MoneyDataHelper.PLACE_TABLE, values, "_id=?", new String[]{acc.getId()+""});
	}
	
	public Place getPlaceById(long id){
		Place acc;
		Cursor cursor = _database.query(MoneyDataHelper.PLACE_TABLE,
				MoneyDataHelper.columns_account, 
				"_id=?", 
				new String[]{String.valueOf(id)}, null, null, null);
		cursor.moveToFirst();
		
		acc = new Place();
		acc.setId(cursor.getLong(0));
		acc.setName(cursor.getString(1));
		acc.setAddress(cursor.getString(2));			
		acc.setLatitude(cursor.getDouble(3));
		acc.setLongitude(cursor.getDouble(4));
		acc.addImage(cursor.getString(5));
		acc.addImage(cursor.getString(6));
		acc.addImage(cursor.getString(7));
		acc.addImage(cursor.getString(8));
		acc.setCategoryId(cursor.getInt(9));
		acc.setCategoryName(cursor.getString(10));
		acc.setDescription(cursor.getString(11));
		acc.setAudio(cursor.getString(12));
		acc.setLastUpdate(cursor.getString(13));
		acc.setChecked(cursor.getInt(14)==1?true:false);
		//Log.i(TAG, String.format("Get account(%d, %s) from database successfully!", acc.getId(), acc.getName()));
		cursor.close();
		return acc;
	}
	
	public ArrayList<Place> getAllPlaces(){
		ArrayList<Place> accs = new ArrayList<Place>();
		Place acc;
		Cursor cursor = _database.query(MoneyDataHelper.PLACE_TABLE, MoneyDataHelper.columns_account, 
				null, null, null, null, null);
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()){
			acc = new Place();
			
			acc.setId(cursor.getLong(0));
			acc.setName(cursor.getString(1));
			acc.setAddress(cursor.getString(2));			
			acc.setLatitude(cursor.getDouble(3));
			acc.setLongitude(cursor.getDouble(4));
			acc.addImage(cursor.getString(5));
			acc.addImage(cursor.getString(6));
			acc.addImage(cursor.getString(7));
			acc.addImage(cursor.getString(8));
			acc.setCategoryId(cursor.getInt(9));
			acc.setCategoryName(cursor.getString(10));
			acc.setDescription(cursor.getString(11));
			acc.setAudio(cursor.getString(12));
			acc.setLastUpdate(cursor.getString(13));
			acc.setChecked(cursor.getInt(14)==1?true:false);
			
			accs.add(acc);
			cursor.moveToNext();
			//Log.i(TAG, String.format("Get account(%d, %s) from database successfully!", acc.getId(), acc.getName()));
		}
		cursor.close();
		return accs;
	}
	
	public ArrayList<String> getAllPlaceName(){
		ArrayList<String> accs = new ArrayList<String>();		
		Cursor cursor = _database.query(MoneyDataHelper.PLACE_TABLE, 
				new String[]{"_name"},null, null, null,null, null);
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()){
			accs.add(cursor.getString(0));
			cursor.moveToNext();
			//Log.i(TAG, String.format("Get account(%d, %s) from database successfully!", acc.getId(), acc.getName()));
		}
		cursor.close();
		return accs;
	}
}
